package com.tjetc.controller;


import com.tjetc.common.JsonResult;
import com.tjetc.service.MerchantFinanceService;
import com.tjetc.service.MerchantStatisticsService;
import org.apache.coyote.http11.Http11InputBuffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;

@RestController
public class StatisticsController {

    @Autowired
    MerchantFinanceService merchantFinanceService;
    @Autowired
    MerchantStatisticsService merchantStatisticsService;

    //获得销量前十的订单的列表
    @RequestMapping("merchant/getDishTop")
    public JsonResult getDishTop(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, HttpSession session){
        Integer id= (Integer) session.getAttribute("id");
        return new JsonResult(0,"查询成功",merchantStatisticsService.getDishTop(begin,end,id));

    }

    //获得用户数量和新用户在一段时间内的统计列表
    @RequestMapping("merchant/getUserNumber")
    public JsonResult getUserNumber(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                    @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, HttpSession session){
        Integer id=(Integer) session.getAttribute("id");
        return new JsonResult(0,"查询成功",merchantStatisticsService.getUserStatistics(begin,end,id));
    }


    //获得在一定时间范围内订单数量
    @RequestMapping("merchant/getOrderNumber")
    public  JsonResult getOrderNumber(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, HttpSession session){
        int id= (int) session.getAttribute("id");
        return new JsonResult<>(0,"查询成功",merchantStatisticsService.getOrderStatistics(begin,end,id));
    }

    //获得一段时间内每日的营业额列表
    @RequestMapping("merchant/getTurnover")
    public JsonResult getTurnover(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, HttpSession session){
        int id= (int) session.getAttribute("id");
        return new JsonResult(0,"查询成功",merchantStatisticsService.getTurnOver(begin,end,id));
    }

    //获得营业额大致统计信息，包括上季度营业额，上月营业额，以及当前账户余额
    @RequestMapping("merchant/getTurnOverDate")
    public JsonResult getTurnoverDate(HttpSession session){
        Integer id= (Integer) session.getAttribute("id");
        return new JsonResult(0,"查询成功",merchantFinanceService.getAmountStatistic(id));
    }

    //获得一定时间范围内每日账单列表
    @RequestMapping("merchant/getDailyBills")
    public JsonResult getDailyBills (@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end, HttpSession session){
        return new JsonResult(0,"获取账单成功",merchantFinanceService.getDailyBill((Integer) session.getAttribute("id"),begin,end));
    }

    //获得给定日期所有的订单
    @RequestMapping("merchant/getOrdersByDay")
    public JsonResult getOrdersByDay(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate date,HttpSession session){
        Integer id= (Integer) session.getAttribute("id");
        return new JsonResult<>(0,"获取订单列表成功",merchantFinanceService.getOrdersByDay(id,date));
    }


}
